package com.xaicode.template.activemq.topic;

import com.activemq.demo.dataobject.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JMS_produce_topic_dcy {


    public static final String ACTIVEMQ_URL = "tcp://192.168.18.182:61616";
    public static final String TOPIC_NAME = "CRZS_ENV_DATA_REPORT";


    /**
     * 1、一次运行订阅此公众号 注册一次 然后关闭程序
     * 2、再次运行依然可以收到持久化消息
     *
     * @param args
     * @throws JMSException
     */
    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = activeMQConnectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(TOPIC_NAME);
        MessageProducer producer = session.createProducer(topic);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        connection.start();
        String msg = createMsg();
        MapMessage mapMessage = session.createMapMessage();
        mapMessage.setObject("Msg", msg);
        producer.send(mapMessage);
        producer.close();
        session.close();
        connection.close();

        System.out.println("持久化TOPIC消息发送完成");
    }


    public static String createMsg() {
        IEMsgHeader ieMsgHeader = new IEMsgHeader();
        ieMsgHeader.setMsgType("CRZS_ENV_DATA_REP");

        IENetDev ieNetDev = new IENetDev();
        ieNetDev.setDevId("600010002");

        IECoordinate ieCoordinate = new IECoordinate();
        ieCoordinate.setXCoordinate("300");
        ieCoordinate.setYCoordinate("100");
        ieCoordinate.setZCoordinate("100");

        IETime ieTime = new IETime();
        ieTime.setYear(2020);
        ieTime.setMonth(11);
        ieTime.setDay(20);
        ieTime.setHour(15);
        ieTime.setMin(45);
        ieTime.setSec(20);

        List<IETemperature> ieTemperature = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            IETemperature temperature = new IETemperature();
            int j = (int) ((Math.random() * 9 + 1) * 100);
            temperature.setValue(j + "");
            ieTemperature.add(temperature);
        }

        List<IEGas> IEGas = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            IEGas gas = new IEGas();
            int j = (int) (Math.random() * 100);
            gas.setSolubility(j + "");
            int type = (int) (Math.random() * 7);
            gas.setType(type);
            IEGas.add(gas);
        }


        List<IEHumidity> IEHumidity = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            IEHumidity ieHumidity = new IEHumidity();
            int j = (int) (Math.random() * 100);
            ieHumidity.setValue(j + "");
            IEHumidity.add(ieHumidity);
        }

        List<IEDust> IEDust = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            IEDust dust = new IEDust();
            int j = (int) (Math.random() * 100);
            dust.setSolubility(j + "");
            int type = (int) (Math.random() * 7);
            dust.setType(type + "");
            IEDust.add(dust);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("IEMsgHeader", ieMsgHeader);
        map.put("IENetDev", ieNetDev);
        map.put("IECoordinate", ieCoordinate);
        map.put("IETime", ieTime);
        map.put("IETemperature", ieTemperature);
        map.put("IEGas", IEGas);
        map.put("IEHumidity", IEHumidity);
        map.put("IEDust", IEDust);
        Gson gson = new GsonBuilder().create();
        String s = gson.toJson(map);
        return s;

    }
}
